package com.guanyong.services;

import com.guanyong.dao.IKeyWordDaoMapper;
import com.guanyong.model.KeywordModel;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by mxk on 2017/7/3.
 */
@Service
public class KeyWordService {

    @Autowired
    IKeyWordDaoMapper iKeyWordDaoMapper;

    public ResultObject<Integer> AddKeyWord(KeywordModel keywordModel){
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        KeywordModel keywordModel1=iKeyWordDaoMapper.selectByKeywrod(keywordModel.getfKeywordmd5());
        if (keywordModel1!=null ){
            Integer res= iKeyWordDaoMapper.UpdateSearchCt(keywordModel1.getfId());
            if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }
        else{
            Integer res= iKeyWordDaoMapper.insert(keywordModel);
            if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }
        return  resultObject;
    }

    public List<KeywordModel> GetHotKeyWord(Integer count){
        return iKeyWordDaoMapper.SelectHotSearch(count);
    }

    public ResultObject<Integer> AddKeyWord1_0(KeywordModel keywordModel) {
        ResultObject<Integer> resultObject=new ResultObject<Integer>();
        KeywordModel keywordModel1=iKeyWordDaoMapper.selectByKeywrod1_0(keywordModel.getfKeywordmd5(),keywordModel.getfUserid());
        if (keywordModel1!=null ){
            Integer res= iKeyWordDaoMapper.UpdateSearchCt1_0(keywordModel1.getfId(),keywordModel.getfUserid());
            if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }
        else{
            Integer res= iKeyWordDaoMapper.insert(keywordModel);
            if (res >-1){
                resultObject.setCode(ResultCode.OperateOk);
            }
            else{
                resultObject.setCode(ResultCode.OperateFail);
            }
        }
        return  resultObject;
    }

    public List<KeywordModel> GetHotKeyWord(Integer count, String userid) {
        return iKeyWordDaoMapper.SelectHotSearch1_0(count,userid);
    }
}
